package com.xie.smfs.manager;

import com.xie.smfs.common.events.PlayerEvents;
import com.xie.smfs.registry.ModEffects;
import com.xie.smfs.registry.ModItems;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.entity.effect.StatusEffectInstance;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.entity.LivingEntity;
import com.xie.smfs.client.data.ClientDataManager;

public class GhostDomainManager {
    private static final int BASE_DOMAIN_RADIUS = 32;
    private static final int LEVEL_RADIUS_BONUS = 16;

    private static int getDomainRadius(int level) {
        return BASE_DOMAIN_RADIUS + (level - 1) * LEVEL_RADIUS_BONUS;
    }
    private static final int LEVEL_1_FLY_SPEED = 0;
    private static final int LEVEL_2_FLY_SPEED = 2;

    public static void toggleGhostDomain(PlayerEntity player) {
        if (!hasValidGhostEye(player)) return;

        int level = getGhostEyeLevel(player);
        boolean hasEffect = player.hasStatusEffect(ModEffects.RED_GHOST_DOMAIN);

        if (hasEffect) {
            disableGhostDomain(player);
        } else {
            enableGhostDomain(player, level);
        }
    }

    public static void enableGhostDomain(PlayerEntity player, int level) {
        if (level <= 0) return;

        player.addStatusEffect(new StatusEffectInstance(
            ModEffects.RED_GHOST_DOMAIN,
            Integer.MAX_VALUE,
            level - 1, // 放大器从0开始
            false, false, true
        ));
        if (player instanceof ServerPlayerEntity serverPlayer) {
            // 获取当前鬼眼所在槽位
            int slotIndex = PlayerEvents.findEquippedGhostEyeSlot(player);
            if (slotIndex != -1) {
                int currentDegree = PlayerEvents.getGhostSlotRevivalDegree(player, slotIndex);
                PlayerEvents.setGhostSlotRevivalDegree(player, slotIndex, currentDegree + 3);
            }
        }

        applyLevelAbilities(player, level);
    }

    public static void disableGhostDomain(PlayerEntity player) {
        player.removeStatusEffect(ModEffects.RED_GHOST_DOMAIN);
        resetLevelAbilities(player);
    }

    private static void applyLevelAbilities(PlayerEntity player, int level) {
        // 1级能力: 无
        // 2级能力: 洞察生物
        if (level >= 2) {
            // 实现生物洞察逻辑
            int radius = getDomainRadius(level);
            player.getWorld().getOtherEntities(player, player.getBoundingBox().expand(radius)).forEach(entity -> {
                if (entity instanceof LivingEntity) {
                    // 标记实体为可被看见
                    entity.setGlowing(true);
                }
            });
        }

        // 3级能力: 飞行
        if (level >= 3) {
            player.getAbilities().allowFlying = true;
            player.getAbilities().flying = true;
            player.sendAbilitiesUpdate();
        }
    }

    private static void resetLevelAbilities(PlayerEntity player) {
        // 关闭所有实体的发光效果
        int radius = getDomainRadius(getGhostEyeLevel(player));
        player.getWorld().getOtherEntities(player, player.getBoundingBox().expand(radius)).forEach(entity -> {
            if (entity instanceof LivingEntity) {
                entity.setGlowing(false);
            }
        });

        // 仅当玩家不是创造模式或旁观者模式时才禁用飞行
        if (!player.isCreative() && !player.isSpectator()) {
            player.getAbilities().allowFlying = false;
            player.getAbilities().flying = false;
            player.sendAbilitiesUpdate();
        }
    }

    public static boolean hasValidGhostEye(PlayerEntity player) {
        for (int i = 0; i < 6; i++) {
            if (PlayerEvents.getGhostSlotItem(player, i).getItem() == ModItems.SILENT_GHOST_EYE) {
                return true;
            }
        }
        return false;
    }

    public static int getGhostEyeLevel(PlayerEntity player) {
        for (int i = 0; i < 6; i++) {
            if (PlayerEvents.getGhostSlotItem(player, i).getItem() == ModItems.SILENT_GHOST_EYE) {
                return PlayerEvents.getGhostSlotLevel(player, i);
            }
        }
        return 0;
    }

    public static boolean isGhostDomainActive(PlayerEntity player) {
        return player.hasStatusEffect(ModEffects.RED_GHOST_DOMAIN);
    }

    public static int getCurrentLevel(PlayerEntity player) {
        if (!isGhostDomainActive(player)) return 0;
        return player.getStatusEffect(ModEffects.RED_GHOST_DOMAIN).getAmplifier() + 1;
    }

    public static boolean isImmuneToGhostDomain(PlayerEntity player, int ghostLevel) {
        return isGhostDomainActive(player) && getCurrentLevel(player) > ghostLevel;
    }
}